package com.sensePlusVR.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.DensityUtil;

/***
 * 自定义圆弧进度条
 * 
 * @author liujing
 */
@SuppressLint("DrawAllocation")
@SuppressWarnings("unused")
public class CircleDLProgressView extends View {
	private static final String TAG = CircleDLProgressView.class.getSimpleName();
	private int[] section_colors  = {getContext().getResources().getColor(R.color.pro_color_start), getContext().getResources().getColor(R.color.pro_color_center),getContext().getResources().getColor(R.color.pro_color_end)};
	private float maxCount = 100.0f;
	private float currentCount;
	private int score;
	private Paint mPaint;
	private Paint mTextPaint;
	private Paint mTitlePaint;
	private Paint mSubtitlePaint;
	private Paint mPauseImagePaint;
	private int mWidth, mHeight;
	private int mStartColor = section_colors[0];
	private int mEndColor = section_colors[2];
	private String mTitle = "Aleady cleaned";
	private String mSubTitle = "500M";
	private boolean mScoreVisible = true;
	private boolean mCenterTextVisible = false;
	private boolean mCenterPauseImageVisible = false;
	private boolean mCenterWaitImageVisible = false;
	private boolean mCenterErrorImageVisible = false;
	private int mScoreSpSize = 25;
	private float mCircleStrokeWidthSp = 2.0f;
	
	
	private int mBackCircleColor = getContext().getResources().getColor(R.color.pro_download_ring_bg);
	//分成10段
	private static final int segment = 100;
	
	private float totalAngle = 360.0f;
	
	private float anglePerSegment = totalAngle/segment;
	
	private float startAnglePosition = 0.0f;
	

	/**
	 * 设置显示环形总角度及开始角度结束角度
	 * @param totalAngle
	 * @param startAngle
	 * @param endAngle
	 */
	public void setAngle(float totalAngle,float startAnglePosition){
		this.totalAngle = totalAngle;
		this.startAnglePosition = startAnglePosition;
		this.anglePerSegment = (totalAngle - startAnglePosition)/segment;
	}
	
	//设置起始颜色
	public void setStartColor(int startColor) {
		this.mStartColor = startColor;
	}

	//设置结束颜色
	public void setEndColor(int endColor) {
		this.mEndColor = endColor;
	}
	
	//设置背景圆圈颜色
	public void setBackCircleColor(int backCircleColor){
		
		this.mBackCircleColor = backCircleColor;
	}
	
	/**
	 * 设置中间百分比字体大小
	 * @param scoreSpSize
	 */
	public void setScoreSpSize(int scoreSpSize){
		
		this.mScoreSpSize = scoreSpSize;
		
		mTextPaint.setTextSize(DensityUtil.sp2px(getContext(),mScoreSpSize));
	}
	
	/**
	 * 设置环形宽度
	 * @param circleStrokeWidthSp
	 */
	public void setCircleStrokeWithSp(float circleStrokeWidthSp){
		
		this.mCircleStrokeWidthSp = circleStrokeWidthSp;
		
		
	}
	
	//设置中间标题
	public void setCenterText(String title,String subTitle){
		
		mTitle = title;
		
		mSubTitle = subTitle;
		
		invalidate();
	}
	
	public void setScoreVisible(boolean visible){
		
		mScoreVisible = visible;
		
		invalidate();
	}
	
	public void setCenterTextVisible(boolean visible){
		
		mCenterTextVisible = visible;
		
		invalidate();
	}
	
	public void setCenterPauseImageVisible(boolean visible){
		mCenterPauseImageVisible = visible;
		
		invalidate();
	}
	
	public void setCenterWaitImageVisible(boolean visible){
		mCenterWaitImageVisible = visible;
		
		invalidate();
	}

	public void setCenterErrorImageVisible(boolean visible){
		mCenterErrorImageVisible = visible;
		
		invalidate();
	}
	/**
	 * 让混合色颠倒
	 */
	public void setMixColorReverse(){
		mStartColor = section_colors[2];
		mEndColor = section_colors[0];
	}
	
	/**
	 * 让进度逆时针旋转
	 */
	public void setAnticlockwise(){
		totalAngle = -360.0f;
		anglePerSegment = totalAngle/segment;
	}
	
	public CircleDLProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	public CircleDLProgressView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CircleDLProgressView(Context context) {
		this(context, null);
	}

	private void init(Context context) {
		mPaint = new Paint();
		mTextPaint = new Paint();
		mTitlePaint = new Paint();
		mSubtitlePaint = new Paint();
		mPauseImagePaint = new Paint();
		
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeCap(Cap.ROUND);

		mTextPaint.setAntiAlias(true);
		mTextPaint.setStrokeWidth((float) 3.0);
		mTextPaint.setTextAlign(Paint.Align.CENTER);
		mTextPaint.setTextSize(DensityUtil.sp2px(getContext(),mScoreSpSize));
		mTextPaint.setColor(getResources().getColor(R.color.pro_download_text));
		
		mTitlePaint.setAntiAlias(true);
		mTitlePaint.setStrokeWidth((float) 2.0);
		mTitlePaint.setTextAlign(Paint.Align.CENTER);
		mTitlePaint.setTextSize(DensityUtil.sp2px(getContext(),18));	
		mTitlePaint.setColor(getResources().getColor(R.color.pro_center_title));
		
		mSubtitlePaint.setAntiAlias(true);
		mSubtitlePaint.setStrokeWidth((float) 4.0);
		mSubtitlePaint.setTextAlign(Paint.Align.CENTER);
		mSubtitlePaint.setTextSize(DensityUtil.sp2px(getContext(),30));	
		mSubtitlePaint.setColor(getResources().getColor(R.color.pro_center_subtitle));
		
		mPauseImagePaint.setAntiAlias(true);
		
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		RectF rectBlackBg = new RectF(20, 20, mWidth - 20, mHeight - 20);
		

		mPaint.setStrokeWidth((float)DensityUtil.dip2px(getContext(),mCircleStrokeWidthSp));

		mPaint.setColor(mBackCircleColor);
		
		canvas.drawArc(rectBlackBg, startAnglePosition, totalAngle, false, mPaint);
		
		float offset = -90.0f;
		float maxAngle = currentCount/maxCount*totalAngle;
		
		float startAngle = 0.0f;
		
		float endAngle = 0.0f;
		
		float alpha = 0.0f;
		
		int StrokePX = DensityUtil.dip2px(getContext(),mCircleStrokeWidthSp*2.0f);
		mPaint.setStrokeWidth((float)StrokePX);
	
		//分段
		for(int i = 0;i < segment;i++){
			
			startAngle = startAnglePosition + i*anglePerSegment;
			
			endAngle = startAnglePosition + (i+1)*anglePerSegment;
			
			if(Math.abs(startAngle)>Math.abs(totalAngle/2)){
				
				alpha = 1.0f - (i-segment/2.0f)*2.0f/segment;
				
			}else{
				
				alpha = i*2.0f/segment;
				
			}
			
			int a = (int) (Color.alpha(mStartColor)*(1-alpha)+Color.alpha(mEndColor)*alpha);
			int r = (int) (Color.red(mStartColor)*(1-alpha)+Color.red(mEndColor)*alpha);
			int g = (int) (Color.green(mStartColor)*(1-alpha)+Color.green(mEndColor)*alpha);
			int b = (int) (Color.blue(mStartColor)*(1-alpha)+Color.blue(mEndColor)*alpha);
			
			mPaint.setColor(Color.argb(a, r, g, b));
			
			if(Math.abs(endAngle)>Math.abs(maxAngle)){
				
				canvas.drawArc(rectBlackBg, offset+startAngle, maxAngle-startAngle, false, mPaint);
				
				
				break;
			}else{
				
				canvas.drawArc(rectBlackBg, offset+startAngle, endAngle - startAngle, false, mPaint);
				
			}
			
			
			
		}
		
		//MyLog.d(TAG, "startAngle : " + startAngle + " endAngle : " + endAngle);
		
		if(mScoreVisible){
		
			
			//解决百分比垂直方向居中
			FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();  
		    float baseline =((rectBlackBg.bottom + rectBlackBg.top - fontMetrics.bottom - fontMetrics.top) / 2);  
		    //绘制百分比Text
			canvas.drawText(score + "%", rectBlackBg.centerX(), baseline, mTextPaint);
				
			
		}
		
		if(mCenterTextVisible){
			
			
		    float baseline = rectBlackBg.centerY() - mTitlePaint.getTextSize(); 
		    //绘制百分比Text
			canvas.drawText(mTitle, rectBlackBg.centerX(), baseline, mTitlePaint);
			
		    baseline = rectBlackBg.centerY() + mSubtitlePaint.getTextSize()*1.0f; 
		    //绘制百分比Text
			canvas.drawText(mSubTitle, rectBlackBg.centerX(), baseline, mSubtitlePaint);
			
		}
		
		if(mCenterPauseImageVisible){
			
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.download_pause_n);
			canvas.drawBitmap(bitmap, rectBlackBg.left + (rectBlackBg.width() - bitmap.getWidth())/2, rectBlackBg.top + (rectBlackBg.height() - bitmap.getHeight())/2, mPauseImagePaint);

		}
		
		if(mCenterWaitImageVisible){
			
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.download_waiting_n);
			canvas.drawBitmap(bitmap, rectBlackBg.left + (rectBlackBg.width())/2 - 8, rectBlackBg.top + (rectBlackBg.height())/2 - bitmap.getHeight() + 8, mPauseImagePaint);
		}
		
		if(mCenterErrorImageVisible){
			
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.download_error_n);
			canvas.drawBitmap(bitmap, rectBlackBg.left + (rectBlackBg.width() - bitmap.getWidth())/2, rectBlackBg.top + (rectBlackBg.height() - bitmap.getHeight())/2, mPauseImagePaint);
			
		}

	}


	
	public int getScore() {
		return score;
	}



	public float getMaxCount() {
		return maxCount;
	}

	public float getCurrentCount() {
		return currentCount;
	}

	public void setScore(int score) {
		this.score = score;
		invalidate();
	}

	/***
	 * 设置最大的进度值
	 * 
	 * @param maxCount
	 */
	public void setMaxCount(float maxCount) {
		this.maxCount = maxCount;
	}

	/***
	 * 设置当前的进度值
	 * 
	 * @param currentCount
	 */
	public void setCurrentCount(float currentCount) {
		this.currentCount = currentCount > maxCount ? maxCount : currentCount;
		invalidate();
	}
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
		if (widthSpecMode == MeasureSpec.EXACTLY
				|| widthSpecMode == MeasureSpec.AT_MOST) {
			mWidth = widthSpecSize;
		} else {
			mWidth = 0;
		}
		if (heightSpecMode == MeasureSpec.AT_MOST
				|| heightSpecMode == MeasureSpec.UNSPECIFIED) {
			mHeight = DensityUtil.dip2px(getContext(), 15);
		} else {
			mHeight = heightSpecSize;
		}
		setMeasuredDimension(mWidth, mHeight);
	}

}
